<?php

require_once './application/core/CoreBase.php';
require_once './application/core/Log/logCore.php';
require_once './application/models/Tables/logFilters.php';
require_once './application/entity/log/namefilter.php';
require_once './application/entity/log/filter.php';

class logFilter extends IndexCore
{

    private $_name;
    private $_data;
    private $_param;
    private $_identity;
    private $_obj;
    private $_check;

    //Cree le filtre
    public function createFilter()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $this->_data = $this->_controller->getRequest()->getParam('data');
        $this->_check = $this->checkPresent();
        $this->_obj = new checkNameFilter();
        $this->_obj->error = $this->_check;
        $this->_obj->name = $this->_name;
        echo Zend_Json_Encoder::encode($this->_obj);
        if ($this->_check == true)
            return false;
        $this->_data = $this->parseArrayFilter();
        $table = new logFilters();
        $table->newLogFilter($this->_name, $this->_data);
        return true;
    }

    public function updateFilter()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $this->_data = $this->_controller->getRequest()->getParam('data');
        $this->_data = $this->parseArrayFilter();
        $table = new logFilters();
        $table->updateLogFilter($this->_name, $this->_data);
    }

    //check si nom de filtre deja present dans la base
    private function checkPresent()
    {
        $table = new logFilters();
        $this->_check = $table->getLogFilter($this->_name);
        if ($this->_check != false)
            return true;
        return false;
    }

    //Parse le retour du visuel
    private function parseArrayFilter()
    {
        $line = null;
        $this->_data = explode(",", $this->_data);
        foreach ($this->_data as $val)
        {
            $val = str_replace("[{", "", $val);
            $val = str_replace("}]", "", $val);
            $line .= $val . ";";
        }
        return $line;
    }

    //Recupere la liste des filtre en base
    public function getListFilterBase()
    {
        $table = new logFilters();
        $this->_data = $table->listLogFilter();
        $this->_obj = new CoreBase($this->_controller);
        $this->_param = $this->_obj->listParamFilter();
        $this->_obj = new coreBase($this->_controller);
        $this->_identity = $this->_obj->listIdentityFilter();
        $this->_obj = new listLogFilter();
        $this->_obj->model = $this->_param['model'];
        $this->_obj->type = $this->_param['type'];
        $this->_obj->protocol = $this->_param['protocol'];
        $this->_obj->filtre = $this->_data;
        $this->_obj->identity = $this->_identity;
        echo Zend_Json_Encoder::encode($this->_obj);
        return true;
    }

    //Affiche le filtre
    public function getFilter()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $this->_check = $this->checkPresent();
        $this->_obj = new checkNameFilter();
        $this->_obj->error = $this->_check;
        echo Zend_Json_Encoder::encode($this->_obj);
        if ($this->_check == false)
            return false;
        return true;
    }

    public function delLog()
    {
        $this->_name = $this->_controller->getRequest()->getParam('filtre');
        $table = new logFilters();
        $table->deleteLogFilter($this->_name);
    }

}
